package com.itestor.iptv.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 
 * @author Mr.li
 * 
 */
public class IpconfigUtil {
    
    private static final Logger logger =Logger.getLogger(IpconfigUtil.class);

    /**
     * 构造器
     */
    public IpconfigUtil() {

    }

    /**
     * 获取系统网卡信息列表
     * @return Map.
     */
    public static Map<String, EthernetAdapter> getEthernetAdapterMap() {
        Map<String, EthernetAdapter> ethernetAdapterMap = new HashMap<String, EthernetAdapter>();
        String os = System.getProperty("os.name");
        if (os != null && os.startsWith("Windows")) {
            try {
                String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                
                EthernetAdapter  adapter=null;
                String readLine = null;
                while ((readLine = br.readLine()) != null) {
                    if (readLine.equals("")){
                         continue;
                    }                    
                    if (readLine.indexOf("Ethernet adapter")>=0){                        
                        if (null!=adapter){
                            ethernetAdapterMap.put(adapter.getPhysicalAddress(), adapter);
                        }                        
                        adapter =new EthernetAdapter();                       
                        int startindex = "Ethernet adapter".length();
                        int endIndex = readLine.indexOf(":");                        
                        String ethernetAdapter= readLine.substring(startindex+1 ,endIndex);                        
                        adapter.setAdapterName(ethernetAdapter);
                        
                    }
                    
                    if (readLine.indexOf("Physical Address") > 0) {
                        int index = readLine.indexOf(":");
                        index += 2;                        
                        adapter.setPhysicalAddress(readLine.substring(index).trim());
                        
                        
                    } else if (readLine.indexOf("DNS Servers") > 0) {
                        int index = readLine.indexOf(":");
                        index += 2;
                        String dns = readLine.substring(index);
                        adapter.setDnsServer(dns.trim());
                    }else if (readLine.indexOf("IP Address") > 0){
                        int index = readLine.indexOf(":");
                        index += 2;
                        String ipAddress = readLine.substring(index);
                        adapter.setIpAddress(ipAddress.trim());
                        
                    }else if (readLine.indexOf("Subnet Mask") > 0){
                        int index = readLine.indexOf(":");
                        index += 2;
                        String subNetMask = readLine.substring(index);
                        adapter.setSubnetMask(subNetMask);                        
                    }else if (readLine.indexOf("Default Gateway")>0){
                        int index = readLine.indexOf(":");
                        index += 2;
                        String getway = readLine.substring(index);
                        adapter.setGateway(getway.trim());
                    }
                    
                }
                br.close();
                return ethernetAdapterMap;
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }

        return ethernetAdapterMap;
    }

    /**
     * 
     * @return String
     */
    public static String getMACAddress() {
        String address = "";
        String os = System.getProperty("os.name");

        if (os != null && os.startsWith("Windows")) {
            try {
                String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.indexOf("Physical Address") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        address = line.substring(index);
                    } else if (line.indexOf("DNS Servers") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        String dns = line.substring(index);
                        System.out.println("dns:" + dns);
                    }
                }
                br.close();
                return address.trim();
            } catch (IOException e) {
            }
        }
        return address;
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
               
            Map<String, EthernetAdapter> map =getEthernetAdapterMap();            
            logger.info(map.get("00-23-4E-E2-56-56").getGateway());
            logger.info(map.get("00-23-4E-E2-56-56").getAdapterName());
            logger.info(map.get("00-23-4E-E2-56-56").getIpAddress());
            logger.info(map.get("00-23-4E-E2-56-56").getSubnetMask());
            logger.info(map.get("00-23-4E-E2-56-56").getPhysicalAddress());
            
   
       
    }
}